<template>
  <editor-layout @ok="handleOk" v-on="$listeners" v-loading="loading">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="handleOk()" label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="typeList[dataForm.type] + '名称'" prop="name">
        <el-input v-model="dataForm.name" :placeholder="typeList[dataForm.type] + '名称'"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type === 1" label="菜单路由" prop="url">
        <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 0" label="授权标识" prop="perms">
        <el-input v-model="dataForm.perms" placeholder="多个用逗号分隔, 如: user:list,user:create"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="orderNum">
        <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="0" label="排序号"></el-input-number>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="菜单图标" prop="icon">
        <image-picker v-model="dataForm.icon"/>
      </el-form-item>

    </el-form>
  </editor-layout>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import {getSysMenuSelect, getSysMenuInfoMenuId, postSysMenuSave, postSysMenuUpdate} from '$my/code/api/sys-menu'
import imagePicker from '@/components/common/image-picker/image-picker'

export default {
  name: 'edit',
  components: {
    imagePicker
  },
  props: {
    id: [String, Number]
  },
  data() {
    // const validateUrl = (rule, value, callback) => {
    //   if (this.dataForm.type === 1 && !/\S/.test(value)) {
    //     callback(new Error('菜单URL不能为空'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loading: false,
      typeList: ['目录', '菜单', '按钮'],
      dataForm: {
        id: 0,
        type: 1,
        name: '',
        parentId: 0,
        parentName: '',
        url: '',
        perms: '',
        orderNum: 0,
        icon: ''
      },
      dataRule: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '上级菜单不能为空', trigger: 'change' }
        ]
      },
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    async init () {
      const menus = await getSysMenuSelect()
      this.menuList = treeDataTranslate(menus.menuList, 'menuId')
      this.$refs.dataForm.resetFields()

      if(!this.id) {
        this.menuListTreeSetCurrentNode()
      } else {
        const data = await getSysMenuInfoMenuId(this.id)
        const menu = data.menu
        this.dataForm = {
          ...menu,
          id: menu.menuId
        }
        this.menuListTreeSetCurrentNode()
      }
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle (data, node) {
      this.dataForm.parentId = data.menuId
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode () {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {}).name
    },
    // 表单提交
    handleOk () {
      this.$refs.dataForm.validate((ok) => {
        if(!ok) return

        let saveFn = postSysMenuSave
        if(this.id) {
          saveFn = postSysMenuUpdate
        }
        const param = {
          ...this.dataForm,
          menuId: this.dataForm.id
        }
        saveFn(param).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.$emit('on-ok')
            }
          })
        })

      })
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.edit {
}
</style>
